package game;

import java.util.Random;

public class Tetromino {    //四方格类 父类
    Cell[] cells = new Cell[4];
    State[] states = new State[4];
    int index = 10000;

    //四方格右旋转
    public void rotateRight() {
        index++;
        State s = states[index % states.length];    //控制数组[]只能是0 1 2 3
        //锁定旋转轴
        Cell c0 = cells[0];
        //0号格子的实际行列
        int row = c0.getRow();
        int col = c0.getCol();
        //根据0号格子的实际行列 以及 1，2，3号格子相对0号的位置计算123真实行列
        cells[1].setRow(row + s.row1);
        cells[1].setCol(col + s.col1);
        cells[2].setRow(row + s.row2);
        cells[2].setCol(col + s.col2);
        cells[3].setRow(row + s.row3);
        cells[3].setCol(col + s.col3);
    }

    //四方格左旋转
    public void rotateLeft() {
        index--;
        State s = states[index % states.length];    //控制数组[]只能是0 1 2 3
        //锁定旋转轴
        Cell c0 = cells[0];
        //0号格子的实际行列
        int row = c0.getRow();
        int col = c0.getCol();
        //根据0号格子的实际行列 以及 1，2，3号格子相对0号的位置计算123真实行列
        cells[1].setRow(row + s.row1);
        cells[1].setCol(col + s.col1);
        cells[2].setRow(row + s.row2);
        cells[2].setCol(col + s.col2);
        cells[3].setRow(row + s.row3);
        cells[3].setCol(col + s.col3);
    }

    //四方格下落
    public void softDrop() {
        for (Cell cell : cells) {
            cell.drop();
        }
    }

    //四方格左移动
    public void moveLeft() {
        for (Cell cell : cells) {
            cell.moveLeft();
        }
    }

    //四方格右移动
    public void moveRight() {
        for (Cell cell : cells) {
            cell.moveRight();
        }
    }

    public static Tetromino randomOne() {
        Random r = new Random();
        int n = r.nextInt(7);   //0~6
        switch (n) {
            case 0:
                return new I();
            case 1:
                return new J();
            case 2:
                return new L();
            case 3:
                return new O();
            case 4:
                return new S();
            case 5:
                return new T();
            case 6:
                return new Z();
            default:
                return null;
        }
    }
}

//每一种四方格的状态
class State {
    int row0, col0, row1, col1, row2, col2, row3, col3;

    //快捷生成构造器
    public State(int row0, int col0, int row1, int col1, int row2, int col2, int row3, int col3) {
        this.row0 = row0;
        this.col0 = col0;
        this.row1 = row1;
        this.col1 = col1;
        this.row2 = row2;
        this.col2 = col2;
        this.row3 = row3;
        this.col3 = col3;
    }
}

class I extends Tetromino {
    public I() {
        //声明一个格子数组
        cells[0] = new Cell(Tetris.I, 0, 4);
        cells[1] = new Cell(Tetris.I, 0, 3);
        cells[2] = new Cell(Tetris.I, 0, 5);
        cells[3] = new Cell(Tetris.I, 0, 6);

        states[0] = new State(0, 0, 0, -1, 0, 1, 0, 2);
        states[1] = new State(0, 0, -1, 0, 1, 0, 2, 0);
        states[2] = new State(0, 0, 0, -1, 0, 1, 0, 2);
        states[3] = new State(0, 0, -1, 0, 1, 0, 2, 0);
    }
}

class J extends Tetromino {
    public J() {
        cells[0] = new Cell(Tetris.J, 0, 4);
        cells[1] = new Cell(Tetris.J, 0, 3);
        cells[2] = new Cell(Tetris.J, 0, 5);
        cells[3] = new Cell(Tetris.J, 1, 5);

        states[0] = new State(0, 0, 0, -1, 0, 1, 1, 1);
        states[1] = new State(0, 0, -1, 0, 1, 0, 1, -1);
        states[2] = new State(0, 0, 0, 1, 0, -1, -1, -1);
        states[3] = new State(0, 0, 1, 0, -1, 0, -1, 1);
    }
}

class L extends Tetromino {
    public L() {
        cells[0] = new Cell(Tetris.L, 0, 4);
        cells[1] = new Cell(Tetris.L, 0, 3);
        cells[2] = new Cell(Tetris.L, 0, 5);
        cells[3] = new Cell(Tetris.L, 1, 3);

        states[0] = new State(0, 0, 0, -1, 0, 1, 1, -1);
        states[1] = new State(0, 0, -1, 0, 1, 0, -1, -1);
        states[2] = new State(0, 0, 0, 1, 0, -1, -1, 1);
        states[3] = new State(0, 0, 1, 0, -1, 0, 1, 1);
    }
}

class O extends Tetromino {
    public O() {
        cells[0] = new Cell(Tetris.O, 0, 4);
        cells[1] = new Cell(Tetris.O, 0, 5);
        cells[2] = new Cell(Tetris.O, 1, 4);
        cells[3] = new Cell(Tetris.O, 1, 5);

        states[0] = new State(0, 0, 0, 1, 1, 0, 1, 1);
        states[1] = new State(0, 0, 0, 1, 1, 0, 1, 1);
        states[2] = new State(0, 0, 0, 1, 1, 0, 1, 1);
        states[3] = new State(0, 0, 0, 1, 1, 0, 1, 1);
    }
}

class S extends Tetromino {
    public S() {
        cells[0] = new Cell(Tetris.S, 0, 4);
        cells[1] = new Cell(Tetris.S, 0, 5);
        cells[2] = new Cell(Tetris.S, 1, 3);
        cells[3] = new Cell(Tetris.S, 1, 4);
        states[0] = new State(0, 0, 0, 1, 1, -1, 1, 0);
        states[1] = new State(0, 0, 1, 0, -1, -1, 0, -1);
        states[2] = new State(0, 0, 0, 1, 1, -1, 1, 0);
        states[3] = new State(0, 0, 1, 0, -1, -1, 0, -1);
    }
}

class T extends Tetromino {
    public T() {
        cells[0] = new Cell(Tetris.T, 0, 4);
        cells[1] = new Cell(Tetris.T, 0, 3);
        cells[2] = new Cell(Tetris.T, 0, 5);
        cells[3] = new Cell(Tetris.T, 1, 4);
        states[0] = new State(0, 0, 0, -1, 0, 1, 1, 0);
        states[1] = new State(0, 0, -1, 0, 1, 0, 0, -1);
        states[2] = new State(0, 0, 0, 1, 0, -1, -1, 0);
        states[3] = new State(0, 0, 1, 0, -1, 0, 0, 1);
    }
}

class Z extends Tetromino {
    public Z() {
        cells[0] = new Cell(Tetris.Z, 1, 4);
        cells[1] = new Cell(Tetris.Z, 0, 3);
        cells[2] = new Cell(Tetris.Z, 0, 4);
        cells[3] = new Cell(Tetris.Z, 1, 5);

        states[0] = new State(0, 0, -1, -1, -1, 0, 0, 1);
        states[1] = new State(0, 0, -1, 1, 0, 1, 1, 0);
        states[2] = new State(0, 0, -1, -1, -1, 0, 0, 1);
        states[3] = new State(0, 0, -1, 1, 0, 1, 1, 0);
    }
}